home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / malloc / malloc.c < prev    next >
C/C++ Source or Header  |  1994-04-21  |  10KB  |  366 lines

  1. /* Memory allocator `malloc'.
  2.    Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation
  3.           Written May 1989 by Mike Haertel.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.
  19.  
  20.    The author may be reached (Email) at the address mike@ai.mit.edu,
  21.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  22.  
  23. #ifndef    _MALLOC_INTERNAL
  24. #define _MALLOC_INTERNAL
  25. #include <malloc.h>
  26. #endif
  27.  
  28. /* How to really get more memory.  */
  29. __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
  30.  
  31. /* Debugging hook for `malloc'.  */
  32. __ptr_t (*__malloc_hook) __P ((size_t __size));
  33.  
  34. /* Pointer to the base of the first block.  */
  35. char *_heapbase;
  36.  
  37. /* Block information table.  Allocated with align/__free (not malloc/free).  */
  38. malloc_info *_heapinfo;
  39.  
  40. /* Number of info entries.  */
  41. static size_t heapsize;
  42.  
  43. /* Search index in the info table.  */
  44. size_t _heapindex;
  45.  
  46. /* Limit of valid info table indices.  */
  47. size_t _heaplimit;
  48.  
  49. /* Free lists for each fragment size.  */
  50. struct list _fraghead[BLOCKLOG];
  51.  
  52. /* Instrumentation.  */
  53. size_t _chunks_used;
  54. size_t _bytes_used;
  55. size_t _chunks_free;
  56. size_t _bytes_free;
  57.  
  58. /* Are you experienced?  */
  59. int __malloc_initialized;
  60.  
  61. void (*__after_morecore_hook) __P ((void));
  62.  
  63. /* Aligned allocation.  */
  64. static __ptr_t align __P ((size_t));
  65. static __ptr_t
  66. align (size)
  67.      size_t size;
  68. {
  69.   __ptr_t result;
  70.   unsigned long int adj;
  71.  
  72.   result = (*__morecore) (size);
  73.   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  74.                           (char *) NULL)) % BLOCKSIZE;
  75.   if (adj != 0)
  76.     {
  77.       adj = BLOCKSIZE - adj;
  78.       (void) (*__morecore) (adj);
  79.       result = (char *) result + adj;
  80.     }
  81.  
  82.   if (__after_morecore_hook)
  83.     (*__after_morecore_hook) ();
  84.  
  85.   return result;
  86. }
  87.  
  88. /* Set everything up and remember that we have.  */
  89. static int initialize __P ((void));
  90. static int
  91. initialize ()
  92. {
  93.   heapsize = HEAP / BLOCKSIZE;
  94.   _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
  95.   if (_heapinfo == NULL)
  96.     return 0;
  97.   memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
  98.   _heapinfo[0].free.size = 0;
  99.   _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
  100.   _heapindex = 0;
  101.   _heapbase = (char *) _heapinfo;
  102.  
  103.   /* Account for the _heapinfo block itself in the statistics.  */
  104.   _bytes_used = heapsize * sizeof (malloc_info);
  105.   _chunks_used = 1;
  106.  
  107.   __malloc_initialized = 1;
  108.   return 1;
  109. }
  110.  
  111. /* Get neatly aligned memory, initializing or
  112.    growing the heap info table as necessary. */
  113. static __ptr_t morecore __P ((size_t));
  114. static __ptr_t
  115. morecore (size)
  116.      size_t size;
  117. {
  118.   __ptr_t result;
  119.   malloc_info *newinfo, *oldinfo;
  120.   size_t newsize;
  121.  
  122.   result = align (size);
  123.   if (result == NULL)
  124.     return NULL;
  125.  
  126.   /* Check if we need to grow the info table.  */
  127.   if ((size_t) BLOCK ((char *) result + size) > heapsize)
  128.     {
  129.       newsize = heapsize;
  130.       while ((size_t) BLOCK ((char *) result + size) > newsize)
  131.     newsize *= 2;
  132.       newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
  133.       if (newinfo == NULL)
  134.     {
  135.       (*__morecore) (-size);
  136.       return NULL;
  137.     }
  138.       memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
  139.       memset (&newinfo[heapsize], 0,
  140.           (newsize - heapsize) * sizeof (malloc_info));
  141.       oldinfo = _heapinfo;
  142.       newinfo[BLOCK (oldinfo)].busy.type = 0;
  143.       newinfo[BLOCK (oldinfo)].busy.info.size
  144.     = BLOCKIFY (heapsize * sizeof (malloc_info));
  145.       _heapinfo = newinfo;
  146.       /* Account for the _heapinfo block itself in the statistics.  */
  147.       _bytes_used += newsize * sizeof (malloc_info);
  148.       ++_chunks_used;
  149.       _free_internal (oldinfo);
  150.       heapsize = newsize;
  151.     }
  152.  
  153.   _heaplimit = BLOCK ((char *) result + size);
  154.   return result;
  155. }
  156.  
  157. /* Allocate memory from the heap.  */
  158. __ptr_t
  159. malloc (size)
  160.      size_t size;
  161. {
  162.   __ptr_t result;
  163.   size_t block, blocks, lastblocks, start;
  164.   register size_t i;
  165.   struct list *next;
  166.  
  167.   /* ANSI C allows `malloc (0)' to either return NULL, or to return a
  168.      valid address you can realloc and free (though not dereference).
  169.  
  170.      It turns out that some extant code (sunrpc, at least Ultrix's version)
  171.      expects `malloc (0)' to return non-NULL and breaks otherwise.
  172.      Be compatible.  */
  173.  
  174. #if    0
  175.   if (size == 0)
  176.     return NULL;
  177. #endif
  178.  
  179.   if (__malloc_hook != NULL)
  180.     return (*__malloc_hook) (size);
  181.  
  182.   if (!__malloc_initialized)
  183.     if (!initialize ())
  184.       return NULL;
  185.  
  186.   if (size < sizeof (struct list))
  187.       size = sizeof (struct list);
  188.  
  189. #ifdef SUNOS_LOCALTIME_BUG
  190.   if (size < 16)
  191.     size = 16;
  192. #endif
  193.  
  194.   /* Determine the allocation policy based on the request size.  */
  195.   if (size <= BLOCKSIZE / 2)
  196.     {
  197.       /* Small allocation to receive a fragment of a block.
  198.      Determine the logarithm to base two of the fragment size. */
  199.       register size_t log = 1;
  200.       --size;
  201.       while ((size /= 2) != 0)
  202.     ++log;
  203.  
  204.       /* Look in the fragment lists for a
  205.      free fragment of the desired size. */
  206.       next = _fraghead[log].next;
  207.       if (next != NULL)
  208.     {
  209.       /* There are free fragments of this size.
  210.          Pop a fragment out of the fragment list and return it.
  211.          Update the block's nfree and first counters. */
  212.       result = (__ptr_t) next;
  213.       next->prev->next = next->next;
  214.       if (next->next != NULL)
  215.         next->next->prev = next->prev;
  216.       block = BLOCK (result);
  217.       if (--_heapinfo[block].busy.info.frag.nfree != 0)
  218.         _heapinfo[block].busy.info.frag.first = (unsigned long int)
  219.           ((unsigned long int) ((char *) next->next - (char *) NULL)
  220.            % BLOCKSIZE) >> log;
  221.  
  222.       /* Update the statistics.  */
  223.       ++_chunks_used;
  224.       _bytes_used += 1 << log;
  225.       --_chunks_free;
  226.       _bytes_free -= 1 << log;
  227.     }
  228.       else
  229.     {
  230.       /* No free fragments of the desired size, so get a new block
  231.          and break it into fragments, returning the first.  */
  232.       result = malloc (BLOCKSIZE);
  233.       if (result == NULL)
  234.         return NULL;
  235.  
  236.       /* Link all fragments but the first into the free list.  */
  237.       for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i)
  238.         {
  239.           next = (struct list *) ((char *) result + (i << log));
  240.           next->next = _fraghead[log].next;
  241.           next->prev = &_fraghead[log];
  242.           next->prev->next = next;
  243.           if (next->next != NULL)
  244.         next->next->prev = next;
  245.         }
  246.  
  247.       /* Initialize the nfree and first counters for this block.  */
  248.       block = BLOCK (result);
  249.       _heapinfo[block].busy.type = log;
  250.       _heapinfo[block].busy.info.frag.nfree = i - 1;
  251.       _heapinfo[block].busy.info.frag.first = i - 1;
  252.  
  253.       _chunks_free += (BLOCKSIZE >> log) - 1;
  254.       _bytes_free += BLOCKSIZE - (1 << log);
  255.       _bytes_used -= BLOCKSIZE - (1 << log);
  256.     }
  257.     }
  258.   else
  259.     {
  260.       /* Large allocation to receive one or more blocks.
  261.      Search the free list in a circle starting at the last place visited.
  262.      If we loop completely around without finding a large enough
  263.      space we will have to get more memory from the system.  */
  264.       blocks = BLOCKIFY (size);
  265.       start = block = _heapindex;
  266.       while (_heapinfo[block].free.size < blocks)
  267.     {
  268.       block = _heapinfo[block].free.next;
  269.       if (block == start)
  270.         {
  271.           /* Need to get more from the system.  Check to see if
  272.          the new core will be contiguous with the final free
  273.          block; if so we don't need to get as much.  */
  274.           block = _heapinfo[0].free.prev;
  275.           lastblocks = _heapinfo[block].free.size;
  276.           if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
  277.           (*__morecore) (0) == ADDRESS (block + lastblocks) &&
  278.           (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
  279.         {
  280.            /* Which block we are extending (the `final free
  281.               block' referred to above) might have changed, if
  282.               it got combined with a freed info table.  */
  283.            block = _heapinfo[0].free.prev;
  284.             _heapinfo[block].free.size += (blocks - lastblocks);
  285.           _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
  286.           continue;
  287.         }
  288.           result = morecore (blocks * BLOCKSIZE);
  289.           if (result == NULL)
  290.         return NULL;
  291.           block = BLOCK (result);
  292.           _heapinfo[block].busy.type = 0;
  293.           _heapinfo[block].busy.info.size = blocks;
  294.           ++_chunks_used;
  295.           _bytes_used += blocks * BLOCKSIZE;
  296.           return result;
  297.         }
  298.     }
  299.  
  300.       /* At this point we have found a suitable free list entry.
  301.      Figure out how to remove what we need from the list. */
  302.       result = ADDRESS (block);
  303.       if (_heapinfo[block].free.size > blocks)
  304.     {
  305.       /* The block we found has a bit left over,
  306.          so relink the tail end back into the free list. */
  307.       _heapinfo[block + blocks].free.size
  308.         = _heapinfo[block].free.size - blocks;
  309.       _heapinfo[block + blocks].free.next
  310.         = _heapinfo[block].free.next;
  311.       _heapinfo[block + blocks].free.prev
  312.         = _heapinfo[block].free.prev;
  313.       _heapinfo[_heapinfo[block].free.prev].free.next
  314.         = _heapinfo[_heapinfo[block].free.next].free.prev
  315.         = _heapindex = block + blocks;
  316.     }
  317.       else
  318.     {
  319.       /* The block exactly matches our requirements,
  320.          so just remove it from the list. */
  321.       _heapinfo[_heapinfo[block].free.next].free.prev
  322.         = _heapinfo[block].free.prev;
  323.       _heapinfo[_heapinfo[block].free.prev].free.next
  324.         = _heapindex = _heapinfo[block].free.next;
  325.       --_chunks_free;
  326.     }
  327.  
  328.       _heapinfo[block].busy.type = 0;
  329.       _heapinfo[block].busy.info.size = blocks;
  330.       ++_chunks_used;
  331.       _bytes_used += blocks * BLOCKSIZE;
  332.       _bytes_free -= blocks * BLOCKSIZE;
  333.     }
  334.  
  335.   return result;
  336. }
  337.  
  338. #ifndef _LIBC
  339.  
  340. /* On some ANSI C systems, some libc functions call _malloc, _free
  341.    and _realloc.  Make them use the GNU functions.  */
  342.  
  343. __ptr_t
  344. _malloc (size)
  345.      size_t size;
  346. {
  347.   return malloc (size);
  348. }
  349.  
  350. void
  351. _free (ptr)
  352.      __ptr_t ptr;
  353. {
  354.   free (ptr);
  355. }
  356.  
  357. __ptr_t
  358. _realloc (ptr, size)
  359.      __ptr_t ptr;
  360.      size_t size;
  361. {
  362.   return realloc (ptr, size);
  363. }
  364.  
  365. #endif
  366.